package com.total.reporting.presentation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javassist.bytecode.Descriptor.Iterator;

import org.hibernate.SQLQuery;
import org.hibernate.Session;

import com.total.reporting.dao.HibernateUtil;
import com.total.reporting.dao.Concept;

public class ConceptController {

	
	public synchronized static List<Concept> getConcepts(String whereClause,
			int pageNumber, int pageSize) {

		Session session = HibernateUtil.currentSession();
		String where = "";
		if (whereClause != null &&whereClause.length()>0)
			where = "where " + whereClause;
		String sql = "select * from Concept " + where + " order by orderNum,concept";
		SQLQuery query = session.createSQLQuery(sql);
		query.addEntity(Concept.class);
		if(pageNumber>-1){
			query.setFirstResult((pageNumber - 1) * pageSize);
			query.setMaxResults(pageSize);
		}
		List<Concept> concepts = query.list();

		return concepts;
	}
	
	public synchronized static List<Concept> getChildrenConcepts(String concept){
		return getConcepts("parent_concept='"+concept+"'",-1,0);
		
	}

	public synchronized static String getFirstChildConceptName(String concept){
		List<Concept> concepts = getConcepts("parent_concept='"+concept+"'",-1,0);
		if(concepts.size()==0)
			return null;
		return concepts.get(0).getConcept();
		
	}
	public synchronized static List<String> getAllConceptNames(String parentConcept){
		List<String> list= new ArrayList<String>();
		List<Concept> concepts = getConcepts("parent_concept='"+parentConcept+"'",-1,0);
		if(concepts.size()==0)
			return list;
		java.util.Iterator<Concept> it= concepts.iterator();
		while(it.hasNext()){
			Concept concept=it.next();
			list.add(concept.getConcept());
		}
		return list;
	}
	
	public synchronized static Map<String,String> getConceptClosedIcons(){
		List<Concept> concepts=getConcepts(null,-1,0);
		Map<String,String> map=new HashMap<String,String>();
		java.util.Iterator<Concept> it= concepts.iterator();
		while(it.hasNext()){
			Concept concept=it.next();
			map.put(concept.getConcept() , concept.getIconClosed());
		}
		return map;

	}
	public synchronized static Map<String,String> getConceptOpenedIcons(){
		List<Concept> concepts=getConcepts(null,-1,0);
		Map<String,String> map=new HashMap<String,String>();
		java.util.Iterator<Concept> it= concepts.iterator();
		while(it.hasNext()){
			Concept concept=it.next();
			map.put(concept.getConcept() , concept.getIconOpened());
		}
		return map;

	}
}
